package com.hboxs.asl.controller.app.product;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Category;
import com.hboxs.asl.entity.Essay;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.service.CategoryService;
import com.hboxs.asl.service.EssayService;
import com.hboxs.asl.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller(value = "appEssayController")
@RequestMapping(value = "/app/essay")
public class EssayController extends BaseController {

    @Resource(name = "essayServiceImpl")
    private EssayService essayService;
    @Resource(name = "categoryServiceImpl")
    private CategoryService categoryService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    /**
     * 文章详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage detail(Long id, HttpServletRequest request) {
        try {
            Essay essay = essayService.find(id);
            if(essay == null) {
                return JsonMessage.error("找不到该文章");
            }
            String path = request.getRequestURL().toString();
            int index = path.lastIndexOf("/detail.htm");
            String contentPath = path.substring(0, index);
            String url =  contentPath + "/essayWeb.htm?essayId="+id;
            return JsonMessage.success(url);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 根据老师id获取文章列表
     * @param teacherId
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage list(Long teacherId, Pageable pageable) {
        try {
            Member teacher = memberService.find(teacherId);
            if(teacher == null) {
                return JsonMessage.error("找不到该老师");
            }
            Page<Essay> page = essayService.findPageByTeacher(pageable, teacher);
            return JsonMessage.success(

                    "totalPage", page.getTotalPages(),

                    "pageNumber", page.getPageNumber(),

                    "pageSize", page.getPageSize(),

                    "total", page.getTotal(),

                    "content", JsonMessage.optimiseObj(
                            page.getContent(),
                            "title", "id", "imageURL", "content","createDateF", "contentWithoutTag", "member.avator"
                    )

            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 根据分类id获取文章列表
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/list_category", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage list_category(Long categoryId, Pageable pageable) {
        try {
            Category category = categoryService.find(categoryId);
            if(category == null) {
                return JsonMessage.error("找不到该分类");
            }
            Page<Essay> page = essayService.findPageByCategory(pageable, category);
            return JsonMessage.success(

                    "totalPage", page.getTotalPages(),

                    "pageNumber", page.getPageNumber(),

                    "pageSize", page.getPageSize(),

                    "total", page.getTotal(),

                    "content", JsonMessage.optimiseObj(
                            page.getContent(),
                            "title", "id", "imageURL", "content", "createDateF", "contentWithoutTag", "member.avator", "member.name"
                    )

            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 分类id获取文章列表
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage get(Long categoryId, Pageable pageable) {
        try {
            Category category = categoryService.find(categoryId);
            if(category == null) {
                return JsonMessage.error("找不到该分类");
            }
            Page<Essay> page = essayService.findPageByCategory(pageable, category);

            return JsonMessage.success(

                    "totalPage", page.getTotalPages(),

                    "pageNumber", page.getPageNumber(),

                    "pageSize", page.getPageSize(),

                    "total", page.getTotal(),

                    "content", JsonMessage.optimiseObj(
                            page.getContent(),
                            "title", "id", "imageURL", "createDateF", "contentWithoutTag", "content"
                    )

            );

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 文章详情
     *
     * @param essayId 文章id
     * @return
     */
    @RequestMapping(value = "/essayWeb", method = RequestMethod.GET)
    public
    String detail(Long essayId, ModelMap modelMap) {
        Essay essay = essayService.find(essayId);
        if (essay == null) {
            return "/yijing/404";
        }
        modelMap.addAttribute("essay", essay);
        return "/yijing/essayWeb";
    }


}
